{extend name="default:layout:base" /}

{block name="main"}
<div class="layui-tab layui-tab-brief" lay-filter="tabBrief">
    <div class="layui-tab-content">
        <form class="layui-form" action="" >
            <input type="hidden" value="{$type}" name="type">
            <div class="layui-row">
                <div class="layui-col-xs7">
                    <div class="layui-col-xs8">
                        <div class="layui-form-item">
                            <input name="search_key" placeholder="请输入文件名称搜索" value="{:input('search_key')}" class="layui-input"
                                   type="text">
                        </div>
                    </div>
                    <div class="layui-col-xs4">
                        <button class="layui-btn layui-btn-primary" lay-submit="" lay-filter="formDemo">
                            <i class="fa fa-search"></i>
                        </button>
                    </div>
                </div>
                <div class="layui-col-xs5 text-right">
                    <input type="checkbox" lay-filter="ids" title="全选" id="ids">
                    <div class="layui-btn-group">
                        <a class="layui-btn  layui-btn-sm layui-btn-danger" onclick="clickDelBatch()">
                            <i class="fa fa-trash-o"></i>批量删除
                        </a>
                        <button type="button" class="layui-btn  layui-btn-sm" id="picker-add">
                            <i class="fa fa-plus"></i>新增文件
                        </button>
                    </div>
                </div>
            </div>

            <div class="layui-row">
                <table class="layui-table" lay-skin="line">
                    <colgroup>
                        <col width="50">
                        <col>
                        <col>
                        <col width="100">
                    </colgroup>
                    <tbody>
                    {volist name="data_list" id="item"}
                    <tr>
                        <td>
                            <input
                                type="checkbox"
                                class="id"
                                lay-filter="id"
                                value="{$item.id}"
                                lay-skin="primary"
                                data-url="{$item.url}"
                                data-title="{$item.title}"
                            />
                        </td>
                        <td>{$item.title}</td>
                        <td><a href="{$item.url}" target="_blank">预览</a></td>
                        <td>
                            <a href="javascript:;" onclick="clickDelOne('{$item.id}')" class="layui-btn layui-btn-danger layui-btn-sm">删除</a>
                        </td>
                    </tr>
                    {/volist}
                    </tbody>
                </table>
            </div>
            <div id="page">
                {$pager|raw}
            </div>
        </form>
    </div>
</div>
<div class="bottom">
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" onclick="clickSure();">确定</button>
            <button class="layui-btn layui-btn-primary js-cancel-btn">取消</button>
        </div>
    </div>
</div>
{/block}

{block name="js"}
<script>
    var parent_index //获取窗口索引
            ,ids = []
            ,$checkBoxes = []
            ,dataLen = parseInt("{:count($data_list)}")
    ,clickDelOne
    , clickDelBatch
    , clickSure;

    layui.use(['jquery','form', 'upload'],function(){
        var form = layui.form
        ,upload = layui.upload
        , $ = layui.jquery;
        //刷新界面 所有元素
        form.render();

        parent_index = parent.layer.getFrameIndex(window.name);

        $checkBoxes = $('.id');
        //点击取消
        $('.js-cancel-btn').on('click', function () {
            parent.layer.close(parent_index);
        });

        //点击删除单个
        clickDelOne = function (id) {
            ids = [id];
            delRequest()
        };
        //点击批量删除
        clickDelBatch = function (id) {
            $.each($('.id:checked'), function(i, item){
                ids.push($(item).val());
            });
            delRequest()
        };

        //点击确认
        clickSure = function () {
            var list = [];
            $('.id:checked').each(function (i, item) {
                list.push({id: $(item).val(), url: $(item).data('url'), title: $(item).data('title')});
            });
            if(list.length < 1){
                layer.alert("请选择文件");
                return;
            }

            parent.setFileValue(list, "{$field}");
            parent.layer.close(parent_index);
        };

        //请求服务端进行删除操作
        var delRequest = function () {
            if(ids.length < 1){
                layer.alert('请选中要删除的数据');
            }
            layer.confirm('删除后无法恢复，你确定吗？', {
                title: '提示',
                btn: ['是','不'] //按钮
            }, function(){
                requestPost("{:url('delPost')}", {'ids': ids, 'type': 'file'});
            });
        };

        //监听全选
        form.on('checkbox(ids)', function(data){
            $checkBoxes.prop('checked', data.elem.checked);
            layui.form.render('checkbox');
        });
        //监听单个选中
        form.on('checkbox(id)', function (data) {
            $("#ids").prop('checked', $('.id:checked').length < dataLen ? false : true);
            layui.form.render('checkbox');
        });

        var upload_index;
        upload.render({
            elem: '#picker-add'
            ,url: '{:url("uploadPost", ["type" => "file"])}'
            ,accept: 'file' //允许上传的文件类型
            ,size: "{$config.file_size ? $config.file_size/1000 : 2048}" //最大允许上传的文件大小
            ,exts: "{$config.file_ext ? $config.file_ext : 'zip,rar,tar,gz,7z,doc,docx,txt,xml,mp3,mp4,xls,xlsx,pdf'}".split(',').join('|')
            ,before: function () {
                upload_index = layer.load(1);
            }
            ,done: function(res, index, upload){ //上传后的回调
                layer.close(upload_index);
                if(res.code === 1){
                    layer.msg("上传成功", {time: 1000}, function () {
                        location.href = location.href.replace(/page=\d+$/g, "page=1"); //回到首页
                    });
                }else{
                    layer.alert(res.msg);
                }
            }
        })
    });
</script>
{/block}

{block name="css"}
<style>
    body{background-color: #fff;}
    .layui-card-body:hover .delete-btn,.layui-card-body:hover .check-btn{display: block;}
    .layui-form-checkbox[lay-skin=primary]{height: 19px !important;margin-top: -12px;margin-left: -1px;}
    #page{margin: 20px auto;text-align: center;}
    .bottom{
        background-color: #f8f8f8;line-height: 52px;height: 52px;position: fixed;text-align: center;
        width: 100%; bottom: 0;
    }
</style>
{/block}